<?php
namespace app\store\controller;

use app\common\common\model\oldModel\store\Store;
use app\common\common\model\table\TableQrcode;
use app\common\common\Server\commonServer;
use think\Request;
use think\Loader;

class User extends Base
{

    public function _initialize(){
        parent::_initialize();
    }


    public function index(){
        $qrcodeModel = new TableQrcode();
        $code = input('code');   //二维码编号
        $this->assign('code' , $code);

        //获取门店名称

        //获取当前登录门店
        $store_id = $this->store_user['store_id'];
        $stoteMember = new Store();
        $store = $stoteMember->where( array('store_id' => $store_id ) )->field('store_id,sname')->find();

        $this->assign('store' , $store);
        //判断是否 存在二维码编号，存在则绑定，不存在则获取所有绑定桌码的信息
        if( $code  ){
            $where['qrcode_sn'] = $code; //二维码编号
            $row = $qrcodeModel->findByWhere($where);
            $this->assign( 'row' , $row);
        } else {

            //获取当前登录门店
            $store_id = $this->store_user['store_id'];
            //获取当前门店 所有已绑定的 桌码
            $list = $qrcodeModel->where( 'sid' , $store_id )->where( 'status' , 2)->order( 'table_id asc ')->select();
            $this->assign( 'list' , $list);
        }

        return $this->view->fetch();
    }




    /**
     * 绑定桌号
     */
    public function bind_table(){

        $code = input('code');   //二维码编号
        $number = input('number/d'); //桌位号
        if( !$code  || !$number ){
            commonServer::error( 2)->dies();
        }
        if( $number < 0 ){
            commonServer::$showMsg = '请输入正确的桌号';
            commonServer::error( 2)->dies();
        }
        $qrcodeModel = new TableQrcode();
        $where['qrcode_sn'] = $code; //二维码编号
        $data = [
            'status'   => 2 ,
            'table_id' => $number ,
            'sid'      => $this->store_user['store_id'] ,
            'bind_time'=> time() ,
            'user_id'  => $this->store_user['id']
        ];
        $result = $qrcodeModel->where( $where )->update(  $data );

        if( $result !==false){
            commonServer::success( )->dies();
        } else {
            $logs = array('event' =>  '/store/user/bind_table' , 'input'=> Request::instance()->param() , 'output' => $result );
            commonServer::error( 99997 , [] , $logs )->dies();
        }


    }
    








}
